layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * 众测与活动管理
     */
    var Activity = {
        tableId: "activityUserTable"
    };

    /**
     * 初始化表格的列
     */
    Activity.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'activityId',  title: '活动id'},
            {field: 'headPic', sort: true, title: '头像',templet:
                    function (d) {
                        return '<div id="photo_big" class="photo_big"><img src="' +d.headPic+'"  style="height: 50px;width: 50px;" /> <div>'
                    }
            ,},
            {field: 'userName', sort: true, title: '用户名'},
            /*{field: 'phone', sort: true, title: '电话'},*/
            {field: 'applyTime', sort: true, title: '申请时间'},
            {field: 'handleTime', sort: true, title: '处理时间'},
            {field: 'handlePerson', sort: true, title: '处理人'},
           /* {align: 'center', toolbar: '#tableBar', title: '操作'}*/
        ]];
    };

    /**
     * 点击查询按钮
     */
    Activity.search = function () {
        var queryData = {};
        queryData['status'] = $("#status").val();
        queryData['title'] = $("#title").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['goodsNo'] = $("#goodsNo").val();
        queryData['id'] = $("#id").val();
        var uploadTime = $("#uploadTime").val();
        if(uploadTime !=""){
            var split = uploadTime.split("-");
            queryData["createTimeMin"]=split[0]
            queryData["createTimeMax"]=split[1]
        }
        table.reload(Activity.tableId, {where: queryData
            ,page:{
                curr:1
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    Activity.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area:["70%","70%"],
            title: '添加众测',
            maxmin:true,
            content: Feng.ctxPath + '/tryOut/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Activity.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Activity.exportExcel = function () {
        var checkRows = table.checkStatus(Activity.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Activity.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改众测',
            area:["70%","70%"],
            maxmin:true,
            content: Feng.ctxPath + '/tryOut/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Activity.tableId);
            }
        });
    };


    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Activity.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/activity/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Activity.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Activity.tableId,
        url: Feng.ctxPath + '/activity/rewardList?id='+Feng.getUrlParam("id"),
        page: true,
        height: "full-58",
        cellMinWidth: 100,
        cols: Activity.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Activity.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Activity.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Activity.exportExcel();
    });

    laydate.render({
        elem: '#uploadTime',
        type:"datetime",
        range:true,
    });

    // 工具条点击事件
    table.on('tool(' + Activity.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Activity.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Activity.onDeleteItem(data);
        }
    });
});
